package net.siufung.simple.limiter;

import com.google.common.util.concurrent.RateLimiter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.boot.redis.service.IRedisClientService;
import net.siufung.web.aop.service.IAutoLimiterService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author 陈建峰
 * @date 2022/10/20 4:12 下午
 * @since V1.0.0
 */
@Slf4j
@Primary
@Component
@AllArgsConstructor
public class AutoLimiterServiceImpl implements IAutoLimiterService {

    private final IRedisClientService redisClientService;

    @Override
    public RateLimiter getCache(String autoLimiterKey) {
        log.info(autoLimiterKey);
        return (RateLimiter)redisClientService.get(autoLimiterKey);
    }

    @Override
    public void putCache(String autoLimiterKey, RateLimiter autoLimiterValue, long timeout, TimeUnit timeUnit) {
        redisClientService.set(autoLimiterKey, autoLimiterValue);
        redisClientService.setExpire(autoLimiterKey, timeout, timeUnit);
    }
}
